<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" label-width="68px">
      <el-form-item label="所属场站" prop="stationName">
        <el-input
          v-model="queryParams.stationName"
          placeholder="请输入场站名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="活动标题" prop="noticeTitle">
        <el-input
          v-model="queryParams.noticeTitle"
          placeholder="请输入活动标题"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="类型" prop="noticeType">
        <el-select v-model="queryParams.status" placeholder="请选择类型" clearable size="small">
          <el-option label="已完成" value="1" />
          <el-option label="未完成" value="0" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>


    <el-table v-loading="loading" :data="activityList" @selection-change="handleSelectionChange">
      <el-table-column label="场站名称" align="center" prop="stationName" />
      <el-table-column label="活动标题" align="center" prop="noticeTitle" :show-overflow-tooltip="true"/>
      <el-table-column label="活动方案" align="center" prop="programNum" :formatter="typeFormat"/>
      <el-table-column label="活动开展" align="center" prop="developNum" :formatter="typeFormat2"/>
      <el-table-column label="活动总结" align="center" prop="summaryNum" :formatter="typeFormat3"/>
      <el-table-column label="发起时间" align="center" prop="createTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-view"
            @click="handleView(scope.row)"
          >活动详细</el-button>
          <el-button v-if="scope.row.id != null && scope.row.id != '' && scope.row.id != undefined"
            size="mini"
            type="text"
            icon="el-icon-view"
            @click="handleView2(scope.row)"
          >详情</el-button>
          <!--<el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['ledger:activity:edit']"
          >修改</el-button>-->
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <el-dialog :visible.sync="dialogVisible">
      <a width="100%" :href="dialogImageUrl" alt="查看附件">查看附件</a>
    </el-dialog>

    <!-- 添加或修改专项活动对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="900px">
      <el-form ref="form"  :model="form" :rules="rules" label-width="150px" style="overflow-y:auto;height:680px;">
        <el-input type="hidden" :value="form.noticeId" prop="noticeId"></el-input>
        <el-form-item label="专项活动主题:" prop="noticeTitle">{{form.noticeTitle}}</el-form-item>
        <el-form-item label="活动方案描述:" prop="programDescription" >
          <el-input v-model="form.programDescription"  type="textarea" :rows="2" ></el-input>
        </el-form-item>
        <el-form-item label="上传活动方案:">
          <el-upload
            :multiple="true"
            accept=".docx, .pdf, .doc, .xls, .xlsx"
            :headers="upload.headers"
            :action="upload.url"
            :file-list="form.fileList"
            list-type="picture-card"
            :limit="upload.limit"
            :before-upload="beforeFilesUpload"
            :on-exceed="exceedFile"
            :on-success="handleFileSuccess"
            :on-preview="handleFilePreview"
            :on-remove="handleRemove">
            <i class="el-icon-plus"></i>
            <div slot="tip" class="el-upload__tip">只能上传pdf/word/xls文件，且不超过20M</div>
          </el-upload>
        </el-form-item>

        <el-form-item label="活动开展描述:" prop="developDescription" >
          <el-input v-model="form.developDescription"  type="textarea" :rows="2" ></el-input>
        </el-form-item>
        <el-form-item label="上传活动开展:">
          <el-upload
            :multiple="true"
            accept=".docx, .pdf, .doc, .xls, .xlsx"
            :headers="upload.headers"
            :action="upload.url"
            :file-list="form.fileList2"
            list-type="picture-card"
            :limit="upload.limit"
            :before-upload="beforeFilesUpload"
            :on-exceed="exceedFile"
            :on-success="handleFileSuccess2"
            :on-preview="handleFilePreview"
            :on-remove="handleRemove2">
            <i class="el-icon-plus"></i>
            <div slot="tip" class="el-upload__tip">只能上传pdf/word/xls文件，且不超过20M</div>
          </el-upload>
        </el-form-item>

        <el-form-item label="活动总结描述:" prop="summaryDescription" >
          <el-input v-model="form.summaryDescription"  type="textarea" :rows="2" ></el-input>
        </el-form-item>
        <el-form-item label="上传活动总结:">
          <el-upload
            :multiple="true"
            accept=".docx, .pdf, .doc, .xls, .xlsx"
            :headers="upload.headers"
            :action="upload.url"
            :file-list="form.fileList3"
            list-type="picture-card"
            :limit="upload.limit"
            :before-upload="beforeFilesUpload"
            :on-exceed="exceedFile"
            :on-success="handleFileSuccess3"
            :on-preview="handleFilePreview"
            :on-remove="handleRemove3">
            <i class="el-icon-plus"></i>
            <div slot="tip" class="el-upload__tip">只能上传pdf/word/xls文件，且不超过20M</div>
          </el-upload>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 详情 -->
    <el-dialog :title="title" :visible.sync="openView" width="900px">
      <el-form ref="form"  :model="form" :rules="rules" label-width="150px" style="overflow-y:auto;height:680px;">
        <el-input type="hidden" :value="form.noticeId" prop="noticeId"></el-input>
        <el-form-item label="专项活动主题:" prop="noticeTitle">{{form.noticeTitle}}</el-form-item>
        <el-form-item label="活动方案描述:" >{{form.programDescription}}</el-form-item>
        <el-form-item label="上传活动方案:">
          <el-upload
            :disabled="true"
            :file-list="form.fileList"
            :class="{disabled:true}"
            list-type="picture-card"
            :on-preview="handleFilePreview">
            <i class="el-icon-plus"></i>
          </el-upload>
        </el-form-item>
        <el-form-item label="活动开展描述:" >{{form.developDescription}}</el-form-item>
        <el-form-item label="上传活动开展:">
          <el-upload
            :disabled="true"
            :file-list="form.fileList2"
            :class="{disabled:true}"
            list-type="picture-card"
            :on-preview="handleFilePreview">
            <i class="el-icon-plus"></i>
          </el-upload>
        </el-form-item>
        <el-form-item label="活动开展描述:" >{{form.summaryDescription}}</el-form-item>
        <el-form-item label="上传活动总结:">
          <el-upload
            :disabled="true"
            :file-list="form.fileList3"
            :class="{disabled:true}"
            list-type="picture-card"
            :on-preview="handleFilePreview">
            <i class="el-icon-plus"></i>
          </el-upload>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="openView = false">关 闭</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listActivity, getActivity, delActivity, addActivity, updateActivity, exportActivity } from "@/api/ledger/activity";
import { getToken } from "@/utils/auth";
export default {
  name: "Activity",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 总条数
      total: 0,
      // 专项活动表格数据
      activityList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      openView: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        stationId: undefined,
        noticeId: undefined,
        stationName:undefined,
        noticeTitle:undefined,
        status:undefined
      },
      imgsList:[],
      upload: {
        // 设置上传的请求头部
        headers: { Authorization: "Bearer " + getToken() },
        // 上传的地址
        url: process.env.VUE_APP_BASE_API + "/common/upload",
        //  文件个数
        limit: 5
      },
      dialogImageUrl: '',
      dialogVisible: false,
      // 表单参数
      form: {
        noticeId:undefined,
        noticeTitle:undefined,
        programDescription:undefined,
        summaryDescription: undefined,
        developDescription: undefined,
        files:[],
        fileList:[],
        files2:[],
        fileList2:[],
        files3:[],
        fileList3:[],
      },
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    const stationId = this.$route.params && this.$route.params.stationId;
    this.queryParams.stationId=stationId;
    const stationName = this.$route.params && this.$route.params.stationName;
    this.queryParams.stationName=stationName;
    this.getList();
  },
  methods: {
    /** 查询专项活动列表 */
    getList() {
      this.loading = true;
      listActivity(this.queryParams).then(response => {
        this.activityList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        stationId: undefined,
        noticeId: undefined,
        delFlag: undefined,
        createTime: undefined,
        createId: undefined,
        updateTime: undefined,
        updateId: undefined,
        remark: undefined,
        programDescription: undefined,
        summaryDescription: undefined,
        developDescription: undefined,
        fileList: [],
        files:[],
        files2:[],
        fileList2:[],
        files3:[],
        fileList3:[],
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.queryParams.status = '';
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!=1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加专项活动";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      if (id != null && id != '' && id != undefined){
        getActivity(id).then(response => {
          this.form = response.data;
          const files = [];
          this.form.fileList.map(function (item) {
            files.push(item.url2);
          });
          this.form.files = files;
          const files2 = [];
          this.form.fileList2.map(function (item) {
            files2.push(item.url2);
          });
          this.form.files2 = files2;
          const files3 = [];
          this.form.fileList3.map(function (item) {
            files3.push(item.url2);
          });
          this.form.files3 = files3;
          this.open = true;
          this.title = "修改专项活动";
        });
      }else {
        console.log(this.form)
        this.form.noticeId = row.noticeId;
        this.form.noticeTitle = row.noticeTitle;
        this.open = true;
        this.title = "修改专项活动";
      }

    },
    /** 提交按钮 */
    submitForm: function() {
      this.$refs["form"].validate(valid => {
        console.log(this.form)
        if (valid) {
          if (this.form.id != undefined) {
            updateActivity(this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              } else {
                this.msgError(response.msg);
              }
            });
          } else {
            addActivity(this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              } else {
                this.msgError(response.msg);
              }
            });
          }
        }
      });
    },
    handleView2(row) {
      this.reset();
      const id = row.id || this.ids
      getActivity(id).then(response => {
        this.id = row.id;
        this.form = response.data;
        const files = [];
        this.form.fileList.map(function (item) {
          files.push(item.url2);
        });
        this.form.files = files;
        const files2 = [];
        this.form.fileList2.map(function (item) {
          files2.push(item.url2);
        });
        this.form.files2 = files2;
        const files3 = [];
        this.form.fileList3.map(function (item) {
          files3.push(item.url2);
        });
        this.form.files3 = files3;
        this.openView = true;
        this.title = "专项活动详情";
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除该条专项活动的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delActivity(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(function() {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有专项活动数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return exportActivity(queryParams);
        }).then(response => {
          this.download(response.msg);
        }).catch(function() {});
    },
    typeFormat(row, column) {
      if (row.programNum > 0){
        return "已完成";
      }
      return "未完成";
    },
    typeFormat2(row, column) {
      if (row.developNum > 0){
        return "已完成";
      }
      return "未完成";
    },
    typeFormat3(row, column) {
      if (row.summaryNum > 0){
        return "已完成";
      }
      return "未完成";
    },
    beforeFilesUpload(file){
      let name = file.name
      let type = name.substring(name.lastIndexOf('.')+1)
      let arry = ["docx","pdf","doc","xls","xlsx"]
      if (arry.indexOf(type) === -1) {
        this.$message.error('文件格式仅支持.docx, .pdf, .doc, .xls, .xlsx格式');
        return false
      }
      const isLt2M = file.size / 1024 / 1024 < 20
      if (!isLt2M) {
        this.$message.error('上传文件大小不能超过 20MB!')
        return false
      }
    },
    exceedFile(files, fileList) {
      let msg = `最多只能上传${this.upload.limit}个文件`;
      this.$message.warning(msg)
    },
    //文件上传
    handleRemove(file, fileList) {
      let i = this.form.files.indexOf(file.url2)
      this.form.files.splice(this.form.files.indexOf(file.url2),1);
      this.form.fileList.splice(i,1);
    },
    handleFilePreview(file) {
      window.open(file.url2);
    },
    handleFileSuccess(response, file) {
      this.form.fileList.push({url: 'http://ycsjoss.oss-cn-hangzhou.aliyuncs.com/weixiu/2020/file/06/89d21521cd4b4e208b1a7f61f39a63bf.jpg',url2:response.fileName})
      this.form.files.push(response.fileName);
    },
    handleRemove2(file, fileList) {
      let i = this.form.files2.indexOf(file.url2)
      this.form.files2.splice(this.form.files2.indexOf(file.url2),1);
      this.form.fileList2.splice(i,1);
    },
    handleFileSuccess2(response, file) {
      this.form.fileList2.push({url: 'http://ycsjoss.oss-cn-hangzhou.aliyuncs.com/weixiu/2020/file/06/89d21521cd4b4e208b1a7f61f39a63bf.jpg',url2:response.fileName})
      this.form.files2.push(response.fileName);
    },
    handleRemove3(file, fileList) {
      let i = this.form.files3.indexOf(file.url2)
      this.form.files3.splice(this.form.files3.indexOf(file.url2),1);
      this.form.fileList3.splice(i,1);
    },
    handleFileSuccess3(response, file) {
      this.form.fileList3.push({url: 'http://ycsjoss.oss-cn-hangzhou.aliyuncs.com/weixiu/2020/file/06/89d21521cd4b4e208b1a7f61f39a63bf.jpg',url2:response.fileName})
      this.form.files3.push(response.fileName);
    },
    //活动详细信息
    handleView(row) {
      this.reset();
      const id = row.noticeId;
      this.$router.push({
        path:"/noticedetail/index",
        query:{
          id
        }
      })
    },
  }
};
</script>
<style  lang="css">
  .disabled .el-upload--picture-card {
    display: none;
  }
</style>
